/**
  (c) Copyright Gavin Bong 2008

  This work ‘as-is’ we provide.
  No warranty express or implied.
  We’ve done our best,
  to debug and test.
  Liability for damages denied.

  Permission is granted hereby,
  to copy, share, and modify.
  Use as is fit,
  free or for profit.
  These rights, on this notice, rely.
  
  http://genaud.net/2005/10/poetic-license/
 */

package net.boingboing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Main
{
    static final Logger logger = LoggerFactory.getLogger( Main.class );
    
    /**
     * Parameters
     * <ul>
     * <li>filename
     * <li>userid
     * <li>password
     * <li>replace?
     * </ul>
     */
    public static void main( String... args )
    {
        if( args.length < 3 )
        {
            logger.info( "Usage: <filename> <userid> <passwd>" );
            System.exit( 1 );
        }

        logger.debug( args[0] );
        logger.debug( args[1] );
        logger.debug( args[2] );

        File file = new File( args[0] );
        if( file.exists() )
        {
            logger.debug( "i can read the file" );
        }
        if( !file.canRead() )
        {
            logger.error( args[0] + " not readable or do not exists" );
            System.exit( 2 );
        }

        //Pattern linePattern = Pattern.compile( "^.*?.\\s(\\w)+\\s(\\d)+.\\s(@(\\w)+\\s)+(\\s)*$" );
        Pattern linePattern = Pattern.compile( "^.*?.\\s(\\w)+\\s(\\d)+.\\s((@(\\w)+\\s)|(@(\\w)+))+(\\s)*$" );

        //Pattern aliasPattern = Pattern.compile( "@(\\w)+\\s" );
        
        Pattern aliasPattern = Pattern.compile( "((@(\\w)+\\s)|(@(\\w)+))" );

        Pattern textPattern = Pattern.compile( "^.*?.\\s(\\w)+\\s(\\d)+.\\s@" );

        BufferedReader reader = null;
        try 
        {
            reader = new BufferedReader( new FileReader( file ) );
            
            BookmarkRepository repository = new BookmarkRepository( args[1], args[2] );
            
            String rawLine = "";
            int path = 0;
            while( (rawLine = reader.readLine()) != null )
            {
                logger.info( rawLine );

                Matcher matcher = linePattern.matcher( rawLine );
                
                Bookmark bookmark = new Bookmark();
                
                if( matcher.matches() )
                {
                    logger.debug( "it is a good quote" );
                    
                    bookmark.init();
                    matcher = textPattern.matcher( rawLine );

                    while( matcher.find() )
                    {
                        String text = matcher.group().trim();
                        logger.info( "\t=>" + text );
                        bookmark.addDescription( text );
                    }

                    matcher = aliasPattern.matcher( rawLine );
                    
                    while( matcher.find() )
                    {
                        String group = matcher.group().trim();
                        logger.info( "\t=>" + group );
                        bookmark.addTag( group );
                    }
                    
                    bookmark.addPath( path );
                    
                    repository.saveBookmark( bookmark );
                    
                    ++path;
                }

            }
        } 
        catch( FileNotFoundException e ) 
        {
            e.printStackTrace();
        } 
        catch( IOException e ) 
        {
            e.printStackTrace();
        }
        finally
        {
            try 
            {
                reader.close();
            } 
            catch( IOException e ) 
            {
                e.printStackTrace();
            }
        }
        
    }
    
    
}
